package at.fhj.itm.game;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import at.fhj.itm.util.Console;

public class QuestionList {

	private StringTokenizer st;
	private File questionsFile;	
	private FileReader fileReader;
	private List<Question> questions;
	
	public QuestionList() {
		questions = new ArrayList<Question>();
	}
	
	// read data from question file
	public List<Question> loadQuestions() {
		String line;
		List<String> tmpList = new ArrayList<String>();
		
		try	{
			questionsFile = new File("Resources/Fragen.txt");
			
			fileReader = new FileReader(questionsFile);
			
			BufferedReader reader = new BufferedReader(fileReader);
			
			while( (line = reader.readLine()) != null) {
				tmpList.add(line);
			}
			
			fileReader.close();
			reader.close();
		}
		catch(Exception e)	{
			Console.write("Fehler: Datei nicht gefunden.");
			return null;
		}
		
		for(String line_ : tmpList) {
			st = new StringTokenizer(line_, ",");
			
			if(st.countTokens() == 6) {
				Question question = new Question(st.nextToken(), st.nextToken(), st.nextToken(), st.nextToken(), st.nextToken(), st.nextToken());
				questions.add(question);
			}
		}
		
		return questions;
	}
}
